﻿///////////////////////////////////////////////////////////////
//                                                           //
// Code from Arcanea Project.                                //
//                                                           //
// Please include this license part to every reproduction    //
//   of code.                                                //
//                                                           //
// This code is under Ms-RL license                          //
//                                                           //
// Please read futher information about the license,         //
//   the project (news and updates) and the                  //
//   contributors on the address :                           //
//     http://www.codeplex.com/Arcanea/                      //
//                                                           //
///////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using Arcanea.Mediator.XmlClasses;
using Arcanea.Mediator;

namespace Arcanea.PacketEditor
{
    public partial class FieldForm : Form
    {
        List<string> allType;

        private XmlField field = null;
        public XmlField Field
        {
            get
            {
                return field;
            }
            set
            {
                UpdateField();
                field = value;
                textBoxName.Text = field.Name;
                textBoxComment.Text = field.Description;
                comboBoxTypeSyst.Text = field.SystemType.ToString();
                checkBoxIDPK.Checked = field.IsIDPK;
                checkBoxNotNull.Checked = field.NotNull;


            }
        }

        public XmlPacket SelectedPacket { get; set; }

        public IList<XmlPacket> AllXmlPackets { get; set; }

        public FieldForm()
        {
            InitializeComponent();
        }

        private void buttonValider_Click(object sender, EventArgs e)
        {
            field = new XmlField();

            field.Name = textBoxName.Text.ToLower();
            field.Description = textBoxComment.Text;
            field.SystemType = comboBoxTypeSyst.SelectedItem.ToString();

            field.IsIDPK = checkBoxIDPK.Checked;
            field.NotNull = checkBoxNotNull.Checked;
        }

        private void FieldForm_Load(object sender, EventArgs e)
        {
            UpdateField();
        }


        bool updated = false;
        private void UpdateField()
        {
            if (!updated)
            {
                //Chargement des types de base Sql et Normaux
                allType = new List<string>();
                allType.AddRange(TypeDatabase.AllBaseTypeStrings);


                if (SelectedPacket != null)
                    allType.Remove(SelectedPacket.Name);

                comboBoxTypeSyst.Items.Clear();
                foreach (String t in allType)
                {
                    comboBoxTypeSyst.Items.Add(t);
                }

                updated = true;
            }
        }

        private void comboBoxTypeSyst_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void checkBoxIDPK_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxIDPK.Checked)
            {
                checkBoxNotNull.Enabled = false;
                checkBoxNotNull.Checked = true;
                comboBoxTypeSyst.SelectedItem = typeof(Int32).ToString();
                comboBoxTypeSyst.Enabled = false;
                textBoxName.Text = "idpk";
                textBoxName.Enabled = false;
                textBoxComment.Text = "idpk de la table.";
            }
            else
            {
                checkBoxNotNull.Enabled = true;
                comboBoxTypeSyst.Enabled = true;
                textBoxName.Text = "";
                textBoxName.Enabled = true;
            }
        }
    }
}
